import util from 'util';

import Book from "./entity/Book";
import Chapter from "./entity/Chapter";
import Content from "./entity/Content";

import tool from "../utils/tool";
import http from "../utils/http";

let URL_SEARCH = "http://api.ixdzs.com/book/search?query=%s";
let URL_BOOK = "http://api.ixdzs.com/book/%s";
let URL_CHAPTERS = "http://api.ixdzs.com/content/%s?view=chapter";
let URL_CONTENT = "http://api.ixdzs.com/chapter/%s";

let strategy = {
    id: 'ixdzs.com',
    name: '爱下电子书(ixdzs.com)',
    homepage: 'https://www.ixdzs.com/',
    search: (keyword) => {
        let fetch_promise = new Promise((resolve, reject) => {
            http.get(util.format(URL_SEARCH, encodeURIComponent(keyword)))
                .then(ret => {
                    let books = [];
                    ret.books.forEach((value, index, array) => {
                        let book = new Book(value._id, value.title, value.author);
                        book.source = strategy.id;
                        books.push(book);
                    });
                    resolve(books);
                })
                .catch(err => {
                    reject(err);
                })
            ;
        });

        return new Promise(async (resolve, reject) => {
            http.fetchWithTimeout(fetch_promise)
                .then(res => resolve(res))
                .catch(err => reject(err))
            ;
        });
    },
    chapters: (bookId) => {
        let fetch_promise = new Promise((resolve, reject) => {
            let url = util.format(URL_CHAPTERS, bookId);
            http.get(url)
                .then(ret => {
                    let arr = [];
                    ret.mixToc.chapters.forEach((value, index, array) => {
                        let c = new Chapter(value.title, value.link);
                        arr.push(c);
                    });
                    resolve(arr);
                })
                .catch(err => {
                    reject(err);
                })
        });

        return new Promise(async (resolve, reject) => {
            http.fetchWithTimeout(fetch_promise)
                .then(res => resolve(res))
                .catch(err => reject(err))
            ;
        });
    },
    content: (link) => {
        let fetch_promise = new Promise((resolve, reject) => {
            http.get(util.format(URL_CONTENT, encodeURIComponent(link)))
                .then(ret => {
                    ret.chapter.body = tool.replace(ret.chapter.body, '　　', '');
                    let c = new Content(ret.chapter.title, ret.chapter.body);
                    resolve(c);
                })
                .catch(err => {
                    reject(err);
                })
        });

        return new Promise(async (resolve, reject) => {
            http.fetchWithTimeout(fetch_promise)
                .then(res => resolve(res))
                .catch(err => reject(err))
            ;
        });
    }
};

export default strategy;
